using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
using static UnityEditor.Progress;
using Scene = UnityEngine.SceneManagement.Scene;

/// <summary>
/// 场景管理器
/// </summary>
public class SceneScriptManager : SingletonManager<SceneScriptManager>
{
    #region 场景管理器字段
    /// <summary>
    /// 当前加载场景
    /// </summary>
    private Scene currentScene;
    public Scene Currentscene
    {
        set
        {
            currentScene = value;
        }
        get
        {
            return currentScene;
        }
    }

    /// <summary>
    /// 场景名字字典
    /// </summary>
    private Dictionary<string, Scene> nameSceneDir;
    public Dictionary<string, Scene> NameSceneDir
    {
        set
        {
            nameSceneDir = value;
        }
        get
        {
            return nameSceneDir;
        }
    }

    /// <summary>
    /// 场景编号字典
    /// </summary>
    private Dictionary<int, Scene> numberSceneDir;
    public Dictionary<int, Scene> NumberSceneDir
    {
        set
        {
            numberSceneDir = value;
        }
        get
        {
            return numberSceneDir;
        }
    }
    #endregion

    #region 场景管理器函数

    /// <summary>
    /// 场景管理器初始化函数
    /// </summary>
    public void Init()
    {
        nameSceneDir=new Dictionary<string, Scene>();
        numberSceneDir=new Dictionary<int, Scene>();
        currentScene=SceneManager.GetActiveScene();
    }

    /// <summary>
    /// 添加场景信息
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="scene">添加场景</param>

    public void AddSceneToDir(string sceneName,Scene scene)
    {
        if(nameSceneDir!=null)
        {
            if (nameSceneDir.ContainsKey(sceneName))
            {
                nameSceneDir[sceneName] = scene;
            }
            else
            {
                nameSceneDir.Add(sceneName, scene);
            }
        }
    }

    /// <summary>
    /// 添加场景信息
    /// </summary>
    /// <param name="sceneNumber">场景编号</param>
    /// <param name="scene">添加场景</param>

    public void AddSceneToDir(int sceneNumber, Scene scene)
    {
        if (numberSceneDir != null)
        {
            if (numberSceneDir.ContainsKey(sceneNumber))
            {
                numberSceneDir[sceneNumber] = scene;
            }
            else
            {
                numberSceneDir.Add(sceneNumber,scene);
            }
        }
    }

    /// <summary>
    /// 加载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="mode">加载场景模式</param>
    public void LoadScene(string sceneName,LoadSceneMode mode=LoadSceneMode.Single)
    {
        SceneManager.LoadScene(sceneName,mode);
        currentScene=SceneManager.GetActiveScene();
    }

    /// <summary>
    /// 加载场景
    /// </summary>
    /// <param name="sceneNumber">场景编号</param>
    /// <param name="mode">加载场景模式</param>
    public void LoadScene(int sceneNumber,LoadSceneMode mode = LoadSceneMode.Single)
    {
        SceneManager.LoadScene(sceneNumber, mode);
        currentScene = SceneManager.GetActiveScene();
    }

    /// <summary>
    /// 卸载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="options">卸载场景模式</param>
    public void UnLoadScene(string sceneName, UnloadSceneOptions options=UnloadSceneOptions.None)
    {
        SceneManager.UnloadSceneAsync(sceneName,options);
    }

    /// <summary>
    /// 卸载场景
    /// </summary>
    /// <param name="sceneNumber">场景编号</param>
    /// <param name="options">卸载场景模式</param>
    public void Un(int sceneNumber, UnloadSceneOptions options = UnloadSceneOptions.None)
    {
        SceneManager.UnloadSceneAsync(sceneNumber, options);
    }

    /// <summary>
    /// 场景管理器清空函数
    /// </summary>
    public void Clear()
    {
        if (nameSceneDir != null)
        {
            nameSceneDir.Clear();
            nameSceneDir = null;
        }
        if (numberSceneDir != null)
        {
            numberSceneDir.Clear();
            numberSceneDir = null;
        }
    }
    #endregion
}
